package com.ocm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ocm.core.mapping.DTOConfiguration;
import com.ocm.core.mapping.IDMapping;
import com.ocm.core.service.ClassDefService;

@Controller
public class ClassDefMenuController {

   public static final String ROOT_ITEMS_TREE_PAGE = "classDef/menu/rootItemsTree";
   public static final String ITEMS_TREE_PAGE = "classDef/menu/ItemsTree";
   
   private ClassDefService classDefService;

   @Autowired
   public ClassDefMenuController(
         @Qualifier("classDefService")ClassDefService classDefService) {
      this.classDefService = classDefService;
   }

   @RequestMapping(value = "/classDef/menu/{storageId}/firstItemsTree", method = RequestMethod.GET)
   public String getRootItemsTree(@PathVariable("storageId") long storageId,ModelMap model) {
      model.addAttribute("classDefs", this.classDefService.findOriginalClass(storageId, DTOConfiguration.instance(IDMapping.CLASSDEF_LIMIT)));
      model.addAttribute("storageId", storageId);
      return ROOT_ITEMS_TREE_PAGE;
   }

   @RequestMapping(value = "/classDef/menu/{parentClassDefId}/itemsTree", method = RequestMethod.GET)
   public String getItemsTree(@PathVariable("parentClassDefId") long parentClassDefId, ModelMap model) {
      model.addAttribute("classDefs", this.classDefService.findClassDefChilds(parentClassDefId, DTOConfiguration.instance(IDMapping.CLASSDEF_LIMIT)));
      return ITEMS_TREE_PAGE;
   }
}
